package com.ncmmall.mall.controller.business.live;

import com.ncmmall.application.business.live.LiveSettingApplication;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.live.LiveSetting;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 直播设置管理
 *
 * @author Samuel
 * @date 2020-9-2 14:18:37
 */
@Controller
@RequestMapping("/mall/live/setting")
@Slf4j
public class LiveSettingController {

    @Autowired
    private LiveSettingApplication liveSettingApplication;

    /**
     * 直播间开通协议
     */
    @RequestMapping(method = RequestMethod.GET, value = "/modifyProtocol")
    public String modifyProtocol(Model model) {
        LiveSetting setting = liveSettingApplication.findLastSetting();
        model.addAttribute("applyNote", setting == null ? "" : setting.getOpenProtocol());
        return "mall/live/setting/openProtocol";
    }

    /**
     * 直播间开通协议
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/modifyProtocol")
    @ResponseBody
    public Map<String, Object> modifyProtocol(String note) {
        try {
            Requires.hasLength(note, "内容不能为空");
            liveSettingApplication.modifyProtocol(note);
            return ImmutableMap.of("status", true, "message", "修改成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[修改直播间开通协议异常]", e);
            return ImmutableMap.of("status", false, "message", "修改异常");
        }
    }

    /**
     * 直播关于我们
     */
    @RequestMapping(method = RequestMethod.GET, value = "/aboutUs")
    public String aboutUs(Model model) {
        LiveSetting setting = liveSettingApplication.findLastSetting();
        model.addAttribute("applyNote", setting == null ? "" : setting.getAboutUs());
        return "mall/live/setting/openProtocol";
    }

    /**
     * 直播关于我们
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/aboutUs")
    @ResponseBody
    public Map<String, Object> aboutUs(String note) {
        try {
            Requires.hasLength(note, "内容不能为空");
            liveSettingApplication.aboutUs(note);
            return ImmutableMap.of("status", true, "message", "修改成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[修改直播关于我们异常]", e);
            return ImmutableMap.of("status", false, "message", "修改异常");
        }
    }

    /**
     * 结算设置
     */
    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(Model model) {
        LiveSetting setting = liveSettingApplication.findLastSetting();
        if (setting == null) {
            setting = new LiveSetting();
        }
        model.addAttribute("setting", setting);
        return "mall/live/setting/form2";
    }

    /**
     * 分销设置
     */
    @RequestMapping(value = "/modify", method = {RequestMethod.PUT, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> modify(LiveSetting params) {
        try {
            liveSettingApplication.modify(params);
            return ImmutableMap.of("status", true, "message", "修改成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[修改直播设置异常]", e);
            return ImmutableMap.of("status", false, "message", "修改异常");
        }
    }

}
